<?php
/**
 * Zhub - A PHP Framework For API Developer
 *
 * This file is part of the Zhub package.
 *
 * (c) YiLong Fan <fantiq@163.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @author fanyilong <fantiq@163.com>
 * @copyright 2017 fanyilong. All rights reserved.
 * @package Zhub
 * @date 18-2-27
 */

namespace Zhub\Prototype\Objective\Clazz\Method;


use Zhub\DataStruct\Struct;
use Zhub\Prototype\Objective\Clazz\Clazz;
use Zhub\Prototype\Objective\Clazz\ClazzUtil;
use Zhub\Prototype\Variable\Type;
use Zhub\Prototype\Variable\TypeEnum;

class ArgumentStruct extends Struct
{
    const TYPE = 'type';

    const NAME = 'name';

    const DEFAULT_VALUE = 'defaultValue';

    public static function initialize(): array
    {
        return [
            self::TYPE => null,
            self::NAME => '',
            self::DEFAULT_VALUE => null,
        ];
    }

    public function getType():?Type
    {
        return $this->get(self::TYPE);
    }

    public function setType(?Type $type)
    {
        $this->set(self::TYPE, $type);
    }

    public function getName():string
    {
        return $this->get(self::NAME);
    }

    public function setName(string $name)
    {
        ClazzUtil::checkName($name);
        $this->set(self::NAME, $name);
    }

    public function getDefaultValue()
    {
        return $this->get(self::DEFAULT_VALUE);
    }

    public function setDefaultValue($defaultValue)
    {
        $this->set(self::DEFAULT_VALUE, $defaultValue);
    }

    public function toString(Clazz $clazz = null): string
    {
        if ($clazz == null) {
            // error !!!
        }
        // uses handle
        $type = '';
        if ($argType = $this->getType()) {
            if ($argType->getId() == TypeEnum::CLAZZ) {
                $fullClassName = $argType->getValue();
                $clazz->uses->put($fullClassName);
                list($className) = ClazzUtil::parseClassNameSpace($fullClassName);
                $type = $className . ' ';
            }
            else {
                $type = $argType->getName() . ' ';
            }
        }

        $def = $this->getDefaultValue();
        return $type . '$' . $this->getName() . ($def === null ? '' : ' = ' . var_export($def, true));
    }
}
